package com.shoeHouse.commodityInfo.service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shoeHouse.commodityInfo.dao.CommodityInfoMapper;
import com.shoeHouse.commodityInfo.model.CommodityInfo;
import com.shoeHouse.commodityInfo.model.CommodityInfoExample;
import com.shoeHouse.commodityInfo.model.CommodityInfoExample.Criteria;
import com.shoeHouse.util.PageHelper;
@Service
public class CommodifyInfoServiceImpl implements CommodifyInfoService{

	@Autowired
	private CommodityInfoMapper commodityInfoMapper;
	@Override
	public void save(CommodityInfo o) {
		// TODO Auto-generated method stub
		commodityInfoMapper.insert(o);
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		String[] id=ids.split(",");
		for(int i=0;i<id.length;i++){
			CommodityInfo commodityInfo=new CommodityInfo();
			commodityInfo.setStatus(Byte.parseByte("1"));
			CommodityInfoExample example=new CommodityInfoExample();
			Criteria criteria=example.createCriteria();
	
	        if(id!=null&&!id.equals("")){
	        	criteria.andIdEqualTo(id[i]);
	        }
			commodityInfoMapper.updateByExampleSelective(commodityInfo, example);
		}
	}

	@Override
	public void update(CommodityInfo o) {
		// TODO Auto-generated method stub
		CommodityInfoExample example=new CommodityInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andIdEqualTo(o.getId());
		commodityInfoMapper.updateByExampleSelective(o, example);
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		
		CommodityInfoExample example=new CommodityInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		int count=commodityInfoMapper.countByExample(example);
		return count;
	}

	@Override
	public List<CommodityInfo> getAll(HttpServletRequest request,CommodityInfo commodityInfo, PageHelper pageHelper) {
		// TODO Auto-generated method stub
		String limit="limit "+(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize()+","+pageHelper.getPageSize();
		CommodityInfoExample example=new CommodityInfoExample();
		example.setOrderByClause("gmt_modified DESC "+limit);
	
		Criteria criteria=example.createCriteria();

		criteria.andStatusEqualTo(Byte.parseByte("0"));
		
		if(commodityInfo.getOrderNum()!=null&&!commodityInfo.getOrderNum().equals("")){
			
			criteria.andOrderNumGreaterThan(commodityInfo.getOrderNum());
		}
		
		String name=request.getParameter("name");
		String endTime=request.getParameter("endTime");
		String beginTime=request.getParameter("beginTime");
		String categoryId=request.getParameter("categoryId");
		if(name!=null){
			try {
				criteria.andNameEqualTo(URLDecoder.decode(name,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		if(beginTime!=null){
			if(endTime!=null){
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),sdf.parse(endTime));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),new Date());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		if(categoryId!=null){
			criteria.andCategoryIdEqualTo(categoryId);
			
		}
		if(null!=commodityInfo.getId()&&!commodityInfo.getId().equals("")){
			criteria.andIdEqualTo(commodityInfo.getId());
			
		}
		if(null!=commodityInfo.getCategoryId()&&!commodityInfo.getCategoryId().equals("")){
			criteria.andCategoryIdEqualTo(commodityInfo.getCategoryId());
			
		}
		if(null!=commodityInfo.getCreateId()&&!commodityInfo.getCreateId().equals("")){
			criteria.andCreateIdEqualTo(commodityInfo.getCreateId());
			
		}
		List<CommodityInfo> list=commodityInfoMapper.selectByExample(example);
		return list;
	
	}

	@Override
	public CommodityInfo getSingle(CommodityInfo commodityInfo) {
		// TODO Auto-generated method stub
		CommodityInfo commodityInfos=commodityInfoMapper.selectByPrimaryKey(commodityInfo.getId());
		return commodityInfos;
	}

	@Override
	public List<CommodityInfo> commodifySearch(String text,PageHelper pageHelper) {
		// TODO Auto-generated method stub
		String limit="limit "+(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize()+","+pageHelper.getPageSize();
		CommodityInfoExample example=new CommodityInfoExample();
		example.setOrderByClause("gmt_modified DESC "+limit);
	
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		
			criteria.andNameLike("%"+text+"%");
			
		
		List<CommodityInfo> list=commodityInfoMapper.selectByExample(example);
		return list;
	}




	
}
